<html>
<head><meta charset="utf-8"><title>How to query rust-analyzer/hir database for pub declarations · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/How.20to.20query.20rust-analyzer.2Fhir.20database.20for.20pub.20declarations.html">How to query rust-analyzer/hir database for pub declarations</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="216809216"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/How%20to%20query%20rust-analyzer/hir%20database%20for%20pub%20declarations/near/216809216" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Spencer Kohan <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/How.20to.20query.20rust-analyzer.2Fhir.20database.20for.20pub.20declarations.html#216809216">(Nov 15 2020 at 19:29)</a>:</h4>
<p>Hello!  I heard people working on rust-analyzer are active here.  Could anyone point me in the right direction for what is the best way to get the public declarations from a crate?</p>
<p>So far I've been combing through the source, and with trial and error I've  been able to load a database using cli::load_cargo and get all the crates:</p>
<div class="codehilite"><pre><span></span><code>use rust_analyzer::cli::load_cargo;

fn main() -&gt; std::io::Result&lt;()&gt; {
    let (host, _vfs) = load_cargo(path.as_path(), false, false).unwrap();
    let db = host.raw_database();
    let crates = Crate::all(db);
}
</code></pre></div>
<p>And it seems like <a href="https://rust-analyzer.github.io/rust-analyzer/hir_def/db/trait.DefDatabase.html#tymethod.crate_def_map"><code>crate_def_map</code></a> is what I want but I can't figure out how to get a DefDatabase instance</p>



<a name="216809829"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/How%20to%20query%20rust-analyzer/hir%20database%20for%20pub%20declarations/near/216809829" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Spencer Kohan <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/How.20to.20query.20rust-analyzer.2Fhir.20database.20for.20pub.20declarations.html#216809829">(Nov 15 2020 at 19:39)</a>:</h4>
<p>it looks like maybe I can upcast a <code>RootDatabase</code> to a <code>DefDatabase</code>, but I can't figure out how to make this work</p>



<a name="216810338"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/How%20to%20query%20rust-analyzer/hir%20database%20for%20pub%20declarations/near/216810338" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/How.20to.20query.20rust-analyzer.2Fhir.20database.20for.20pub.20declarations.html#216810338">(Nov 15 2020 at 19:47)</a>:</h4>
<p><code>DefDatabase</code> is just a trait implemented by the <code>RootDatabase</code> so you should be able to just use the functions defined on the trait on the <code>RootDatabase</code> instance you have there. If you have the trait imported in scope that is.</p>



<a name="216810586"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/How%20to%20query%20rust-analyzer/hir%20database%20for%20pub%20declarations/near/216810586" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Spencer Kohan <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/How.20to.20query.20rust-analyzer.2Fhir.20database.20for.20pub.20declarations.html#216810586">(Nov 15 2020 at 19:50)</a>:</h4>
<p>Thanks, that seems to work.  Maybe this is a dumb question, but what's the right way to get a CrateId from a Crate?  I'm running into a private field error</p>



<a name="216810958"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/How%20to%20query%20rust-analyzer/hir%20database%20for%20pub%20declarations/near/216810958" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/How.20to.20query.20rust-analyzer.2Fhir.20database.20for.20pub.20declarations.html#216810958">(Nov 15 2020 at 19:56)</a>:</h4>
<p>I believe you can't go from <code>Crate</code> to <code>CrateId</code> but instead of using <code>Crate::all</code> you might wanna use the <a href="https://rust-analyzer.github.io/rust-analyzer/base_db/trait.SourceDatabase.html#tymethod.crate_graph"><code>SourceDb::crate_graph</code></a> method in conjunction with the <a href="https://docs.rs/ra_ap_base_db/0.0.23/ra_ap_base_db/struct.CrateGraph.html#method.iter">CrateGraph::iter</a> method.</p>



<a name="216810982"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/How%20to%20query%20rust-analyzer/hir%20database%20for%20pub%20declarations/near/216810982" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/How.20to.20query.20rust-analyzer.2Fhir.20database.20for.20pub.20declarations.html#216810982">(Nov 15 2020 at 19:56)</a>:</h4>
<p>That will give you all the crate ids, if you then want to get a <code>Crate</code> from said id <del>you can query that from the database again with the id</del> you  can just index the <code>CrateGraph</code> with the crate id</p>



<a name="216811025"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/How%20to%20query%20rust-analyzer/hir%20database%20for%20pub%20declarations/near/216811025" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/How.20to.20query.20rust-analyzer.2Fhir.20database.20for.20pub.20declarations.html#216811025">(Nov 15 2020 at 19:57)</a>:</h4>
<p><code>SourceDatabase</code> is, like I believe all Database traits, also implemented for <code>RootDatabase</code></p>



<a name="216811207"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/How%20to%20query%20rust-analyzer/hir%20database%20for%20pub%20declarations/near/216811207" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Spencer Kohan <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/How.20to.20query.20rust-analyzer.2Fhir.20database.20for.20pub.20declarations.html#216811207">(Nov 15 2020 at 20:00)</a>:</h4>
<p>Ok thanks, I will try that</p>



<a name="216811246"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/How%20to%20query%20rust-analyzer/hir%20database%20for%20pub%20declarations/near/216811246" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Spencer Kohan <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/How.20to.20query.20rust-analyzer.2Fhir.20database.20for.20pub.20declarations.html#216811246">(Nov 15 2020 at 20:01)</a>:</h4>
<p>Am I going about this the right way?  I mean if my goal is to get all the public declarations for a crate, does it make sense to go through crate_def_map or is there another API/entry-point which would be better for this?</p>



<a name="216811675"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/How%20to%20query%20rust-analyzer/hir%20database%20for%20pub%20declarations/near/216811675" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/How.20to.20query.20rust-analyzer.2Fhir.20database.20for.20pub.20declarations.html#216811675">(Nov 15 2020 at 20:11)</a>:</h4>
<p>I don't think so, but I also don't know the code base well enough to say this with certainty. Going through the crate_def_map should work though as you have everything you need there</p>



<a name="216811991"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/How%20to%20query%20rust-analyzer/hir%20database%20for%20pub%20declarations/near/216811991" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/How.20to.20query.20rust-analyzer.2Fhir.20database.20for.20pub.20declarations.html#216811991">(Nov 15 2020 at 20:18)</a>:</h4>
<p>You can also check out the code that builds the <code>ImportMap</code> from the <code>CrateDefMap</code>, it also looks for public items</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>